<template>
    <Title :level="2">演示</Title>
    <Form :label-col="labelCol" :wrapper-col="wrapperCol">
        <FormItem label="Activity name" required>
            <Input v-model:value="modelRef.name" />
        </FormItem>
        <FormItem label="Activity zone" required>
            <Select v-model:value="modelRef.region" placeholder="please select your zone">
                <SelectOption value="shanghai">Zone one</SelectOption>
                <SelectOption value="beijing">Zone two</SelectOption>
            </Select>
        </FormItem>
        <FormItem label="Activity type" required>
            <CheckboxGroup v-model:value="modelRef.type">
                <Checkbox value="1" name="type">Online</Checkbox>
                <Checkbox value="2" name="type">Promotion</Checkbox>
                <Checkbox value="3" name="type">Offline</Checkbox>
            </CheckboxGroup>
        </FormItem>
        <FormItem class="error-infos" :wrapper-col="{ span: 14, offset: 4 }" v-bind="errorInfos">
            <Button type="primary" @click.prevent="onSubmit">Create</Button>
            <Button style="margin-left: 10px" @click="resetFields">Reset</Button>
        </FormItem>
    </Form>
</template>
<script setup>
import { reactive, toRaw, computed } from 'vue';
import { toArray } from 'lodash-es';
import { Form, Typography, FormItem, Input, Button, Select, SelectOption, CheckboxGroup, Checkbox } from 'ant-design-vue';
import useDataFormObserve from '@/hooks/useVueDataFormObserve';
const { Title } = Typography
const useForm = Form.useForm;
const labelCol = { span: 4 };
const wrapperCol = { span: 14 };
const modelRef = reactive({

});
useDataFormObserve(modelRef)
const rulesRef = reactive({
    name: [
        {
            required: true,
            message: 'Please input name',
        },
    ],
    region: [
        {
            required: true,
            message: 'Please select region',
        },
    ],
    type: [
        {
            required: true,
            message: 'Please select type',
            type: 'array',
        },
    ],
});


const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef);
const onSubmit = () => {
    validate()
        .then(() => {
            console.log(toRaw(modelRef));
        })
        .catch(err => {
            console.log('error', err);
        });
};
const errorInfos = computed(() => {
    return mergeValidateInfo(toArray(validateInfos));
});
</script>
<style scoped>
.error-infos :deep(.ant-form-explain) {
    white-space: pre-line;
}
</style>